package soporte.web;

/**
 * @project DLC Final / UTN FRC 2013
 * @date Febrero de 2013
 *
 * @author Gonzalo Arribere
 * @author Gustavo Morales
 * @author Pablo Moreno
 */
public class Pagination {

    public static final int RESULTS_PER_PAGE = 10;
    private int mPage;
    private int mInitIndex;
    private int mEndIndex;
    private int mMaxPage;

    public static int calculateMaxPages(int pTotalRows) {
        return (int) Math.ceil((double) pTotalRows / Pagination.RESULTS_PER_PAGE);
    }

    public Pagination(int pPageNumber) {
        mPage = pPageNumber;

        if (mPage <= 0) {
            mPage = 1;
        }
    }

    public Pagination(int pInitIndex, int pEndIndex) {
        mInitIndex = pInitIndex;
        mEndIndex = pEndIndex;
    }

    public void fixEndIndex(int pArrayLength) {
        if (mEndIndex > (pArrayLength - 1)) {
            mEndIndex = pArrayLength - 1;
        }
    }

    public void buildPagination(int pTotalRows) {
        mMaxPage = (int) Math.ceil((double) pTotalRows / Pagination.RESULTS_PER_PAGE);

        if (mPage > mMaxPage) {
            mPage = mMaxPage;
        }

        mInitIndex = RESULTS_PER_PAGE * (mPage - 1);
        mEndIndex = RESULTS_PER_PAGE * mPage - 1;
    }

    public int getPage() {
        return mPage;
    }

    public int getInitIndex() {
        return mInitIndex;
    }

    public int getEndIndex() {
        return mEndIndex;
    }

    public static void main(String[] args) {
        Pagination p = new Pagination(10);
        p.buildPagination(103);
    }
}